import http from '../../../common/httpUtil';
import * as types from './suggestionMutationTypes';

const actions = {
  querySuggestions({commit}, {pageIndex = 1, pageSize = 10} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_BEGIN);

    return http.get(`userfeedback/all?pageIndex=${pageIndex}&pageSize=${pageSize}`)
      .then(data => {
        console.log(data)
        for (var i = 0; i < data.length; i++) {
          if (data[i].feedbackIsResolve === '0') {
            data[i].feedbackIsResolve = '未解决'
          } else if (data[i].feedbackIsResolve === '1') {
            data[i].feedbackIsResolve = '解决'
          }
          if (data[i].feedbackType === '0') {
            data[i].feedbackType = '无效'
          } else if (data[i].feedbackType === '1') {
            data[i].feedbackType = '有效'
          }
        }
        var files = data
        commit(types.SYS_FILE_UPLOAD_QUERY_FILES_SUC, {files});
      });
  },
  querySuggestionsCount({commit}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_BEGIN);

    return http.get('userfeedback/count').then(data => {
      var totalCount = data
      console.log(data)
      commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_SUC, {totalCount})
    });
  },
  searchSuggestions({commit}, {feedbackType, pageIndex = 1, pageSize = 10} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_BEGIN);

    return http.get(`userfeedback/all?pageIndex=${pageIndex}&pageSize=${pageSize}&feedbackType=${feedbackType}`)
      .then(data => {
        console.log(data)
        for (var i = 0; i < data.length; i++) {
          if (data[i].feedbackIsResolve === '0') {
            data[i].feedbackIsResolve = '未解决'
          } else if (data[i].feedbackIsResolve === '1') {
            data[i].feedbackIsResolve = '解决'
          }
          if (data[i].feedbackType === '0') {
            data[i].feedbackType = '无效'
          } else if (data[i].feedbackType === '1') {
            data[i].feedbackType = '有效'
          }
        }
        var files = data
        commit(types.SYS_FILE_UPLOAD_QUERY_FILES_SUC, {files});
      });
  },
  searchSuggestionsCount({commit}, {feedbackType}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_BEGIN);
    return http.get(`userfeedback/count?feedbackType=${feedbackType}`).then(data => {
      var totalCount = data
      console.log(data)
      commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_SUC, {totalCount})
    });
  },
  editSuggesion({commit}, {formData}) {
    return http.post(`userfeedback/update?feedbackId=${formData.feedbackId}&feedbackType=${formData.feedbackType}&feedbackIsResolve=${formData.feedbackIsResolve}&feedbackResolve=${formData.feedbackResolve}`).then(data => {
      console.log(data)
      return data
    });
  }
};


export default actions;
